package apps.MyJMRI;

import java.awt.Dimension;
import java.util.HashMap;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.Map.Entry;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import jmri.ShutDownTask;

import wiiremotej.WiiRemoteJ;

import lionel.MyBrowser.MyBrowserJmriJFrame;
import lionel.MyController.MyController;
import lionel.MyController.MyControllersDiscoverer;
import lionel.MyController.MyControllersListener;
import lionel.MyJMRI.MyTrain.MyTrain;
import lionel.MyJMRI.MyTrain.MyTrains;
import lionel.MyJMRI.MyTrain.MyLocomotives;
import lionel.MyJMRI.MyTrain.MyTrainsInstance;

public class MyFirstBrowserJFrame extends MyBrowserJmriJFrame implements MyControllersListener, ShutDownTask {
	private static final long serialVersionUID = 2168941098545983001L;
	static org.apache.log4j.Category log = org.apache.log4j.Category.getInstance(MyFirstBrowserJFrame.class.getName());	

	HashMap<String, MyController> ctrls;
	
	public MyFirstBrowserJFrame(String name) {
		super(name);
		setTitle("No controller detected");
		ImageIcon imageIcon = new ImageIcon(getClass().getResource("NoControl.jpg"));
		JLabel splashSc = new JLabel(imageIcon);
		splashSc.setBounds(0, 0, imageIcon.getIconWidth(), imageIcon.getIconHeight());
		splashSc.setPreferredSize( new Dimension (imageIcon.getIconWidth(), imageIcon.getIconHeight()));
		this.setContentPane(splashSc);
		this.setPreferredSize( splashSc.getPreferredSize() );
		this.setResizable(false);
		this.pack();
		jmri.InstanceManager.shutDownManagerInstance().register(this);
		ctrls = new HashMap<String, MyController>();
		new MyControllersDiscoverer(this);
	}
	
	public void dispose()
	{
		log.debug("Dispose");
		execute();
		jmri.InstanceManager.shutDownManagerInstance().deregister(this);
		super.dispose();
	}

	public boolean execute()
	{
		log.debug("Shutdown manager start");
		// Stop wiiremote search
		if (WiiRemoteJ.isFindInProgress())
			WiiRemoteJ.stopFind();
		// Shutdown active devices
		if (!ctrls.isEmpty())
		{
			Iterator<Entry<String, MyController>> ite = ctrls.entrySet().iterator();
			while (ite.hasNext())
			{
				ite.next().getValue().shutdown();
			}
		}
		// Shutdown trains
		SortedSet<MyTrain> myTrains = MyTrainsInstance.getInstance();
		if (!myTrains.isEmpty())
		{
			Iterator<MyTrain> ite = myTrains.iterator();
			while (ite.hasNext())
			{
				ite.next().getConductor().releaseThrottle();
			}
		}
		return true;
	}
	
	public synchronized void addDevice(MyController controller) {
		ctrls.put(controller.getID(), controller);
		if (mController == null)
		{
			mController = controller;
			controller.isBrowser(this);
			mLst1 = new MyLocomotives();
			mLst2 = new MyTrains();
			mCurElt = null;
			lst1Next();
			return;
		}
		// already got a control, need another frame
		MyBrowserJmriJFrame f = new MyBrowserJmriJFrame(new MyLocomotives(), new MyTrains(), controller);
		controller.isBrowser(f);
		f.pack();
		Dimension screen = f.getToolkit().getScreenSize();
		f.setLocation(screen.width*2/5,screen.height*2/5);
		f.setVisible(true); 
	}

	public String name() {
		return "MyFirstBrowserJFrame::MyJMRIShutdownTask";
	}
}
